// All nodes' values are different, positive and less than 10000

#include <iostream>
#include <string>
#include <cstdio>
#include <sstream>

using namespace std;

int inOrder[10000], postOrder[10000], pos[10000];
int n, minSum, result;

void dfs(int li, int ri, int lp, int rp, int sum)
{
	//cout << li << " " << ri << " " << lp << " " << rp << " " << sum << endl;

	if (li > ri) return;

	if (li == ri)
	{
		if (minSum > sum + inOrder[li])
		{
			minSum = sum + inOrder[li];
			result = inOrder[li];
		}
		return;
	}

	int v = postOrder[rp];
	dfs(li, pos[v]-1, lp, lp + (pos[v]-1-li), sum + v);
	dfs(pos[v]+1, ri, lp + (pos[v]-li), rp-1, sum + v);
}

int main()
{
	string s;
	while (getline(cin, s))
	{
		stringstream ss(s);

		n = 0;
		int p;
		while (ss >> p)
		{
			pos[p] = n;
			inOrder[n++] = p;
		}

		for (int i = 0; i < n; i++) cin >> postOrder[i];
		getline(cin, s);

		minSum = 1000000000;
		dfs(0, n-1, 0, n-1, 0);

		cout << result << endl;
	}
	return 0;
}
